package com.mes.service;

import com.mes.entity.EmesoJobOrder;
import com.mes.entity.EmesoJobOrderExe;
import com.mes.entity.EmesoManuOrder;
import com.mes.entity.EmesoManuOrderJobOrder;
import com.yy.base.common.dao.Dao;
import com.yy.base.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 生产报工
 */
@Service
@SuppressWarnings("unchecked")
public class InformWorkService {
	@Autowired
	private Dao dao;
	/**
	 * 获取作业订单信息
	 */
	public EmesoManuOrderJobOrder getJobOrderInfo(String orderCode) {
		String sql = "select emeso_manuorder_job_order_id id, code, process_Time processTime," +
				"status, qty, qty_Done qtyDone, qty_Start qtyStart, " +
				"route_no routeNo, route_name routeName, exe_Date_From exeDateFrom, exe_Date_To exeDateTo," +
				"Emeso_ManuOrder_Job_Id orderJobId, Emeso_ManuOrder_Id maunOrderId, is_Quality isQuality " +
			" from emeso_manuorder_job_order " +
			"where code = ?";
		return ObjectUtil.mapToEntity(EmesoManuOrderJobOrder.class, dao.queryUnique(sql, orderCode));
	}
	/**
	 * 获取生产订单工序信息
	 */
	public EmesoJobOrder getOrderJobInfo(String id) {
		String sql = "select " +
				"emeso_manuorder_job_id id," +
				"qty," +
				"qty_Done qtyDone," +
				"qty_Start qtyStart," +
				"status," +
				"exe_Date_From exeDateFrom," +
				"exe_Date_To exeDateTo," +
				"emeso_manuorder_id manuOrderId " +
			" from emeso_manuorder_job " +
			"where emeso_manuorder_job_id = ?";
		return ObjectUtil.mapToEntity(EmesoJobOrder.class, dao.queryUnique(sql, id));
	}
	/**
	 * 获取生产订单信息
	 */
	public EmesoManuOrder getManuOrderInfo(String id) {
		String sql = "select " +
				"emeso_manuorder_id id," +
				"code," +
				"qty," +
				"status," +
				"p.value productCode," +
				"p.name productName," +
				"p.name2 productName2 " +
		    " from m_product p, emeso_manuorder m " +
		    "where p.m_product_id = m.m_product_id" +
			"  and emeso_manuorder_id = ?";
		return ObjectUtil.mapToEntity(EmesoManuOrder.class, dao.queryUnique(sql, id));
	}
	/**
	 * 保存作业报工信息信息
	 */
	public void saveJobExeInfo(EmesoJobOrderExe jobExe) {
		String sql = "insert into emeso_manuorder_job_exe (" +
			"emeso_manuorder_job_exe_id," +
			"code," +
			"emeso_manuorder_job_order_id," +
			"ad_user_Id," +
			"exe_Date_From," +
			"exe_Date_To," +
			"exe_Time," +
			"qty," +
			"qty_Done," +
			"status," +
			"is_Quality," +
			"code_Start," +
			"code_End," +
			"description," +
			"ad_client_id," +
			"ad_org_id," +
			"createdBy," +
			"updatedBy)" +
		"values (?,generatecode('emeso_manuorder_job_exe','code','')," +
			"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		dao.executeSql(sql, jobExe.getId(), jobExe.getJobOrderId(), jobExe.getUserId(),
				jobExe.getExeDateFrom(), jobExe.getExeDateTo(), jobExe.getExeTime(),
				jobExe.getQty(), jobExe.getQtyDone(), jobExe.getStatus(), jobExe.getIsQuality(),
				jobExe.getCodeStart(), jobExe.getCodeEnd(), jobExe.getDescription(),
				jobExe.getClientId(), jobExe.getOrgId(),
				jobExe.getCreatedBy(), jobExe.getUpdatedBy());
	}
	/**
	 * 更新生产订单信息
	 */
	public void updateJobOrderInfo(EmesoManuOrderJobOrder jobOrder) {
		List params = new ArrayList();
		String sql = "update emeso_manuorder_Job_Order " +
				"set status = ?, qty_Done = ?, qty_Start = ?,updated = now()";
		params.add(jobOrder.getStatus());
		params.add(jobOrder.getQtyDone());
		params.add(jobOrder.getQtyStart());

		if (jobOrder.getExeDateFrom() != null) {
			sql += ",exe_Date_From = ?";
			params.add(jobOrder.getExeDateFrom());
		}
		if (jobOrder.getExeDateTo() != null) {
			sql += ",exe_Date_To = ?";
			params.add(jobOrder.getExeDateTo());
		}
		sql += " where emeso_manuorder_Job_Order_id = ?";
		params.add(jobOrder.getId());

		dao.executeSql(sql, params.toArray());
	}
	/**
	 * 更新作业计划信息(派工表)
	 */
	public void updateOrderJobInfo(EmesoJobOrder orderJob) {
		List params = new ArrayList();
		String sql = "update emeso_manuorder_Job " +
			"set status = ?, qty_Done = ?, qty_Start = ?, updated = now() ";
		params.add(orderJob.getStatus());
		params.add(orderJob.getQtyDone());
		params.add(orderJob.getQtyStart());
		if (orderJob.getExeDateFrom() != null) {
			sql += ",exe_Date_From = ?";
			params.add(orderJob.getExeDateFrom());
		}
		if (orderJob.getExeDateTo() != null) {
			sql += ",exe_Date_To = ?";
			params.add(orderJob.getExeDateTo());
		}
		sql += " where emeso_manuorder_Job_id = ?";
		params.add(orderJob.getId());
		
		dao.executeSql(sql, params.toArray());
	}
	
	/**
	 * 更新订单状态(通过订单的不同状态向上更新订单状态)
	 */
	public void syncUpdateOrderStatus() {
		dao.executeSql("{call Emes_Update_Order_Status}");
	}
	/**
	 * 获取可开工数量
	 */
	public long getCanStartWork(String orderId, Long routeNo) {
		String sql = "select Emes_Get_Can_Start_Work(?, ?) count0 from dual";
		Map map = dao.queryUnique(sql, orderId, routeNo);
		if (map != null) {
			return ((BigDecimal)map.get("COUNT0")).longValue();
		}
		return 0;
	}
}